package com.redmoon.weixin.mgr;

import cn.js.fan.util.ErrMsgException;
import cn.js.fan.util.StrUtil;
import com.cloudweb.oa.entity.Department;
import com.cloudweb.oa.exception.ValidateException;
import com.cloudweb.oa.service.IDepartmentService;
import com.cloudweb.oa.service.IDeptUserService;
import com.cloudweb.oa.utils.SpringUtil;
import com.cloudwebsoft.framework.util.LogUtil;
import com.redmoon.oa.dept.DeptDb;
import com.redmoon.oa.dept.DeptUserDb;
import com.redmoon.oa.organization.DeptTreeAction;
import com.redmoon.oa.person.UserDb;

import java.util.HashMap;

/**
 * 用于OA中 用户 部门 新增的一系列操作
 */
public class UserDeptByWXMgr {
    public interface Callback {
        void createDeptAfter();
    }

    /**
     * 获取微信数据 创建用户至OA
     *
     * @param hashMap
     * @throws ErrMsgException
     */
    public void createUser(HashMap<String, String> hashMap) throws ErrMsgException {
        String userId = hashMap.get("UserID");
        UserDb userDb = new UserDb();
        userDb = userDb.getUserDbByWeixin(userId);
        if (userDb.isLoaded()) {
            saveUser(hashMap);
            if (hashMap.containsKey("Department")) {
                //先删除
                IDeptUserService deptUserService = SpringUtil.getBean(IDeptUserService.class);
                deptUserService.delOfUser(userDb.getName());

                String deptIds = hashMap.get("Department");
                createDeptUser(userDb.getName(), deptIds);
            }
        } else {
            String realName = hashMap.get("Name");
            String mobile = hashMap.get("Mobile");
            String deptIds = hashMap.get("Department");
            com.redmoon.oa.security.Config scfg = com.redmoon.oa.security.Config.getInstance();
            // 默认密码
            String defaultPwd = scfg.getInitPassword();
            userDb.create(userId, realName, defaultPwd, mobile, DeptDb.ROOTCODE);//新增用户
            userDb.setWeixin(userId);
            userDb.save();
            userDb = userDb.getUserDbByWeixin(userId);

            saveUser(hashMap);//修改用户
            createDeptUser(userDb.getName(), deptIds);//创建用户部门
        }
    }

    public void saveUser(HashMap<String, String> hashMap) throws ErrMsgException {
        String userId = hashMap.get("UserID");
        UserDb userDb = new UserDb();
        userDb = userDb.getUserDbByWeixin(userId);
        if (userDb.isLoaded()) {
            if (hashMap.containsKey("Email")) {
                userDb.setEmail(hashMap.get("Email"));
            }
            if (hashMap.containsKey("Gender")) {
                userDb.setGender(StrUtil.toInt(hashMap.get("Gender")) == 1 ? 0 : 1);
            }
            if (hashMap.containsKey("Telephone")) {
                userDb.setPhone(hashMap.get("Telephone"));
            }
            if (hashMap.containsKey("Name")) {
                userDb.setRealName(hashMap.get("Name"));
            }
            userDb.save();
        }
    }

    /**
     * 创建部门用户
     *
     * @param deptIds
     */
    public void createDeptUser(String userId, String deptIds) throws ErrMsgException {
        String[] departments = deptIds.split(",");
        if (departments.length > 0) {
            for (String deptIdStr : departments) {
                DeptDb deptDb;
                int deptId = StrUtil.toInt(deptIdStr);
                deptDb = deptId == 1 ? new DeptDb(DeptDb.ROOTCODE) : new DeptDb(deptId);
                if (deptDb.isLoaded()) {
                    DeptUserDb _deptUserDb = new DeptUserDb();
                    _deptUserDb.create(deptDb.getCode(), userId, "");
                }
            }
        }
    }

    /**
     * 创建部门
     *
     * @param hashMap
     */
    public boolean createDept(HashMap<String, String> hashMap) throws ErrMsgException {
        boolean flag = true;
        int Id = StrUtil.toInt(hashMap.get("Id"), 0);
        int parentId = StrUtil.toInt(hashMap.get("ParentId"), 0);
        if (Id == 1) {
            return true;
        }
        String Name = hashMap.get("Name");
        DeptDb parentDept = null;
        parentDept = parentId == 1 ? new DeptDb(DeptDb.ROOTCODE) : new DeptDb(parentId);
        if (parentDept.isLoaded()) {
            DeptDb cDept = new DeptDb(Id);
            if (cDept.isLoaded()) {//修改
                cDept.setParentCode(parentDept.getCode());
                cDept.setName(Name);
                flag = cDept.save();
            } else { //新增
                DeptTreeAction deptTreeAction = new DeptTreeAction();
                deptTreeAction.generateNewNodeCode(parentDept.getCode());
                String newCode = deptTreeAction.getNewNodeCode();
                Department lf = new Department();
                lf.setId(Id);
                lf.setName(Name);
                lf.setCode(newCode);
                lf.setParentCode(parentDept.getCode());
                lf.setDescription("");
                lf.setDeptType(1);
                lf.setIsShow(1);
                lf.setShortName(Name);
                lf.setIsGroup(0);
                lf.setIsHide(0);
                IDepartmentService departmentService = SpringUtil.getBean(IDepartmentService.class);
                try {
                    flag = departmentService.create(lf);
                } catch (ValidateException e) {
                    LogUtil.getLog(getClass()).error(e);
                    throw new ErrMsgException(e.getMessage());
                }
            }
        }
        return flag;
    }

}
